﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Honestysoft.Baseframework.WebUI;

namespace Honestysoft.Financing.WebUI.Module.Contract
{
    /// <summary>
    /// ContractTree 的摘要说明
    /// </summary>
    public class ContractTree : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string parentId = context.Request.Params["ID"];
            List<Honestysoft.Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode> lsNode = new List<Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode>();
            string responseData = "";
            Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode childNode = null;
            if (parentId == null)
            {
                childNode = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                childNode.ID = "0";
                childNode.Text = "项目树";
                childNode.NodeAttributes = new Dictionary<string, string>();
                childNode.NodeAttributes.Add("URL", "BD_Contract_List.aspx?ID=0&NodeType=0");
                childNode.ChildCount = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ProjectCategory").Count;
                lsNode.Add(childNode);
            }
            else
            {                
                if (parentId == "0")
                {
                    List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ProjectCategory");
                    DataTable dt = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectCategoryCount(null);
                    DataRow[] rows = null;
                    foreach (Honestysoft.DictionaryHelper.Sys_Dictionary item in lsDict)
                    {
                        childNode = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                        childNode.ID = item.DictValue;
                        childNode.Text = item.DictName;
                        childNode.NodeAttributes = new Dictionary<string, string>();
                        childNode.NodeAttributes.Add("URL", "BD_Contract_List.aspx?ID=" + childNode.ID + "&NodeType=1&ParentID="+parentId);
                        rows = dt.Select("ProjectCategory='" + item.DictValue + "'");
                        if (rows != null && rows.Length == 1)
                        {
                            childNode.ChildCount = Convert.ToInt32(rows[0]["ChildNodes"]);
                        }
                        lsNode.Add(childNode);
                    }
                }
                else
                {
                    List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ProjectCategory");
                    List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsContractDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ContractCategory");
                    if (lsDict != null && lsDict.Find((Honestysoft.DictionaryHelper.Sys_Dictionary find) => find.DictValue == parentId) != null)
                    {
                        DataTable dt = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectsByCategory(parentId,null);
                        foreach (DataRow dr in dt.Rows)
                        {
                            childNode = new JQueryEasyUI.Tree.AsynTreeNode();
                            childNode.ID = dr["ID"].ToString();
                            childNode.Text = dr["SN"].ToString() + " " + dr["Name"].ToString();
                            childNode.NodeAttributes = new Dictionary<string, string>();
                            childNode.NodeAttributes.Add("URL", "BD_Contract_List.aspx?ID=" + childNode.ID + "&NodeType=2&ParentID=" + parentId);
                            childNode.ChildCount = lsContractDict.Count;
                            lsNode.Add(childNode);
                        }
                    }
                    else
                    {
                        
                        foreach (Honestysoft.DictionaryHelper.Sys_Dictionary item in lsContractDict)
                        {
                            childNode = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                            childNode.ID = item.DictValue;
                            childNode.Text = item.DictName;
                            childNode.NodeAttributes = new Dictionary<string, string>();
                            childNode.NodeAttributes.Add("URL", "BD_Contract_List.aspx?ID=" + childNode.ID + "&NodeType=3&ParentID="+parentId);
                            childNode.ChildCount = 0;
                            lsNode.Add(childNode);
                        }
                    }
                }
            }
            if (lsNode.Count > 0)
            {
                responseData = Baseframework.WebUI.JQueryEasyUI.Tree.GetAsynTreeJson(lsNode);
            }
            context.Response.Write(responseData);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}